package com.longcai.controller.scm;



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.nearby.ClyjNearbyShops;
import com.longcai.entity.scm.ClyjCoupon;
import com.longcai.service.nearby.ClyjNearbyShopsService;
import com.longcai.service.scm.ClyjCouponDetailService;
import com.longcai.service.scm.ClyjCouponService;
import com.longcai.util.TokenUtils;


/**
 * <p>
 * 礼品券表 前端控制器
 * </p>
 *
 * @author lwk
 * @since 2019-04-29
 */
@Controller
@RequestMapping("/clyjCoupon")
public class ClyjCouponController {
    @Autowired
    private ClyjCouponDetailService couponDetailService;
    @Autowired
    private ClyjCouponService couponService;
    @Autowired
    private ClyjNearbyShopsService shopsService;
    @Autowired
    private TokenUtils tokenUtil;
    
    /**
     * 查询用户的所有礼品券
     * @param token
     * @return
     */
    @RequestMapping(value="getCoupon")
    @ResponseBody
    public ApiResult getCoupon(@RequestBody JSONObject params) {
    	String token = params.getString("token");
    	if (token == null || token.equals("")) {
            return ApiResult.fail("请先登录");
        }
    	try{
	        ClyjUser clyjUser = tokenUtil.Itoken(token);
	        //获取手机号
	        String phone = clyjUser.getPhone();
	        //根据手机号查询礼品券
	        QueryWrapper<ClyjCoupon> couponQuery = new QueryWrapper<ClyjCoupon>();
	        couponQuery.eq("user_phone", phone);
	        List<ClyjCoupon> couponList = couponService.selectList(couponQuery);
	        //获取店铺名称
	        for (ClyjCoupon clyjCoupon : couponList) {
	        	QueryWrapper<ClyjNearbyShops> shopsQuery = new QueryWrapper<ClyjNearbyShops>();
	        	shopsQuery.eq("mich_id", clyjCoupon.getShId());
	        	ClyjNearbyShops shops = shopsService.selectOne(shopsQuery);
	        	if(shops==null)
	        		continue;
	        	clyjCoupon.setShName(shops.getShopsName());
			}
	        return ApiResult.success(couponList);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ApiResult.fail("获取错误"+e.getMessage());
    	}
       
    }
}

